﻿using Microsoft.IdentityModel.Logging;
using StudentManage.ViewModels.Base;
using StudentManage.ViewModels.UControls;
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Input;
using Zhy.BLL;
using Zhy.DAL;
using Zhy.DAL.Models;

namespace StudentManage.ViewModels
{
    public class StudentMainViewModel : ViewModelBase
    {
        /// <summary>
        /// 操作数据库上下文
        /// </summary>
        ZhyDbContext context = new ZhyDbContext();
        /// <summary>
        /// 学生类操作数据库对象
        /// </summary>
        StudentBLL stuBLL = new StudentBLL();
        public StudentMainViewModel(string userName)
        {
            try
            {
                UserName = userName;
                StudentList = GetStudentList();
                CurStudentInfo = StudentList[0];
                InitToolBarInfo();
            }
            catch (Exception ex) 
            {
                
                MessageBox.Show(ex.Message);
            }
        }

        private string userName;
        /// <summary>
        /// 用户名
        /// </summary>
        public string UserName
        {
            get { return userName; }
            set
            {
                userName = value;
                OnPropertyChanged();
            }
        }
        /// <summary>
        /// 当前时间
        /// </summary>
        public string CurTime
        {
            get
            {
                return DateTime.Now.ToString();
            }
        }
        /// <summary>
        /// 版权
        /// </summary>
        public string RightInfo
        {
            get
            {
                return "朝夕教育所有";
            }
        }

        private string keywords;
        /// <summary>
        /// 关键词
        /// </summary>
        public string KeyWords
        {
            get { return keywords; }
            set
            {
                keywords = value;
                OnPropertyChanged();
            }
        }

        private int classId;
        /// <summary>
        /// 选择的班级编号
        /// </summary>
        public int ClassId
        {
            get { return classId; }
            set
            {
                classId = value;
                OnPropertyChanged();
            }
        }

        /// <summary>
        /// 班级列表
        /// </summary>
        public List<ViewClassInfo> ClassList
        {
            get
            {
                List<ViewClassInfo> classList = context.ViewClassInfo.ToList();
                classList.Insert(0, new ViewClassInfo() { ClassId = 0, ClassName = "请选择" });
                return classList;
            }
        }

        private bool isShowDel;
        /// <summary>
        /// 是否已删除
        /// </summary>
        public bool IsShowDel
        {
            get { return isShowDel; }
            set
            {
                isShowDel = value;
                OnPropertyChanged();
            }
        }

        private ToolBarViewModel toolBarInfo;
        /// <summary>
        /// 工具栏视图
        /// </summary>
        public ToolBarViewModel ToolBarInfo
        {
            get { return toolBarInfo; }
            set
            {
                toolBarInfo = value;
            }
        }

        private StudentInfoViewModel curStudentInfo;
        /// <summary>
        /// 当前学生信息项
        /// </summary>
        public StudentInfoViewModel CurStudentInfo
        {
            get { return curStudentInfo; }
            set
            {
                curStudentInfo = value;
                OnPropertyChanged();
            }
        }

        private ObservableCollection<StudentInfoViewModel> studentList;
        /// <summary>
        /// 学生列表项信息-集合数据变更可以通知客户端
        /// </summary>
        public ObservableCollection<StudentInfoViewModel> StudentList
        {
            get { return studentList; }
            set
            {
                studentList = value;
                OnPropertyChanged();
            }
        }

        /// <summary>
        /// 查询学生信息命令
        /// </summary>
        public ICommand FindStudentsCmd
        {
            get
            {
                return new RelayCommand(o =>
                {
                    //执行查询方法
                    ReloadStudentList();
                });
            }
        }

        /// <summary>
        /// 刷新学生信息列表
        /// </summary>
        private void ReloadStudentList()
        {
            //执行查询
            StudentList = GetStudentList();
            //刷新工具栏的命令
            ToolBarInfo.OnCanExecuted();
        }

        /// <summary>
        /// 查询学生信息
        /// </summary>
        /// <returns></returns>
        public ObservableCollection<StudentInfoViewModel> GetStudentList()
        {
            ObservableCollection<StudentInfoViewModel> reList = new ObservableCollection<StudentInfoViewModel>();
            //查询学生信息
            List<StudentInfo> list = stuBLL.GetStudentList(KeyWords, ClassId, IsShowDel);
            list.ForEach(s => reList.Add(new StudentInfoViewModel() { StuInfo = s }));
            return reList;
        }

        /// <summary>
        /// 显示学生信息页面
        /// </summary>
        /// <param name="stuId"></param>
        /// <param name="actType"></param>
        private void ShowStudentInfoPage(int stuId, int actType)
        {
            StudentInfoViewModel stuVM = new StudentInfoViewModel(stuId, actType);
            //订阅刷新列表页的事件
            stuVM.ReloadList += ReloadStudentList;
            ShowDialog("stuInfo", stuVM);
        }

        /// <summary>
        /// 初始ToolBarInfo属性
        /// </summary>
        private void InitToolBarInfo()
        {
            ToolBarInfo = new ToolBarViewModel()
            {
                //打开学生新增页面
                AddCmd = new RelayCommand(o =>
                {
                    ShowStudentInfoPage(0, 1);
                }, b =>
                {
                    return !IsShowDel;
                }),
                //打开修改信息页面
                EditCmd = new RelayCommand(o =>
                {
                    if (CurStudentInfo != null)
                    {
                        ShowStudentInfoPage(CurStudentInfo.StuId, 2);
                    }
                }, b =>
                {
                    return StudentList.Count > 0 && (!IsShowDel);
                }),
                //查看命令
                InfoCmd = new RelayCommand(o =>
                {
                    if (CurStudentInfo != null)
                    {
                        ShowStudentInfoPage(CurStudentInfo.StuId, 4);//4代表详情页
                    }
                }, b =>
                {
                    return StudentList.Count > 0;
                }),
                DelCmd = new RelayCommand(o =>
                {
                    if (CurStudentInfo != null)
                    {
                        DeleteStudent(CurStudentInfo.StuId, 1);//逻辑删除
                    }
                }, b =>
                {
                    return StudentList.Count > 0 && (!IsShowDel);
                }),
                RecoverCmd = new RelayCommand(o =>
                {
                    if (CurStudentInfo != null)
                    {
                        DeleteStudent(CurStudentInfo.StuId, 0);//恢复
                    }
                }, b =>
                {
                    return StudentList.Count > 0 && IsShowDel;
                }),
                RemoveCmd = new RelayCommand(o =>
                {
                    if (CurStudentInfo != null)
                    {
                        DeleteStudent(CurStudentInfo.StuId, 2);//移除
                    }
                }, b =>
                {
                    return StudentList.Count > 0 && IsShowDel;
                }),
            };
        }

        /// <summary>
        /// 学生信息删除、恢复、移除处理
        /// </summary>
        /// <param name="stuId"></param>
        /// <param name="isDeleted"></param>
        private async void DeleteStudent(int stuId, int isDeleted)
        {
            string typeName = "删除";
            if (isDeleted == 0)
                typeName = "恢复";
            else if (isDeleted == 2)
            {
                typeName = "移除";
            }
            string msgTitle = $"学生{typeName}";
            if (ShowQuestion(msgTitle, $"你确定要{typeName}该学生信息吗？") == System.Windows.MessageBoxResult.Yes)
            {
                bool bl = false;//执行结果
                switch (isDeleted)
                {
                    case 1:
                        bl = await stuBLL.DeleteStudent(stuId, 1);
                        break;
                    case 0:
                        bl = await stuBLL.DeleteStudent(stuId, 0);
                        break;
                    case 2:
                        bl = await stuBLL.RemoveStudent(stuId);
                        break;
                    default: break;
                }
                if (bl)
                {
                    ShowInfo(msgTitle, $"学生信息{typeName}成功！");
                    ReloadStudentList();
                }
                else
                {
                    ShowError(msgTitle, $"学生信息{typeName}失败！");
                    return;
                }
            }
        }
    }
}
